home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1992 / number1 / extkey.pas < prev    next >
Pascal/Delphi Source File  |  1992-01-29  |  6KB  |  130 lines

  1. UNIT ExtKey;                                { Author: Ron Aaron }
  2. {===============================================================}
  3. { Implements the low-level keyboard handler needed to support   }
  4. { extended keyboards, and a replacement for Drivers.GetKeyEvent.}
  5. { This unit is hereby put in the public domain by its author,   }
  6. {===============================================================}
  7. INTERFACE
  8. USES Drivers;
  9. CONST
  10. {---------------------------------------------------------------}
  11. { ExtKeyBoardInstalled : Set TRUE by unit initialization code   }
  12. {                        if an enhanced keyboard was detected.  }
  13. {---------------------------------------------------------------}
  14.   ExtKeyBoardInstalled : BOOLEAN = FALSE;
  15. {---------------------------------------------------------------}
  16. { Set to True if you don't want to distinguish the 'PAD' keys.  }
  17. {---------------------------------------------------------------}
  18.   FilterDuplicateKeys  : BOOLEAN = FALSE;
  19. {---------------------------------------------------------------}
  20. { Extended Keyboard constants. Not an exhaustive list, however! }
  21. {---------------------------------------------------------------}
  22.    kbF11       = $8500;      kbF12        = $8600;
  23.    kbAltF11    = $8B00;      kbAltF12     = $8C00;
  24.    kbCtrlF11   = $8900;      kbCtrlF12    = $8A00;
  25.    kbShiftF11  = $8700;      kbShiftF12   = $8800;
  26.    kbCtrlUp    = $8DE0;      kbCtrlDn     = $91E0;
  27.    kbPadCtrlUp = $8D00;      kbPadCtrDn   = $9100;
  28.    kbAltUp     = $9800;      kbAltDn      = $A000;
  29. {---------------------------------------------------------------}
  30. { Public declaration so we can use this proc elsewhere.         }
  31. { Same heading as the equivalent in DRIVERS.TPU.                }
  32. {---------------------------------------------------------------}
  33. PROCEDURE GetKeyEvent(VAR Event: TEvent);
  34. {===============================================================}
  35. IMPLEMENTATION
  36.  
  37. CONST
  38. {---------------------------------------------------------------}
  39. { Default INT 16h function numbers, for use with older, 84-key  }
  40. { keyboards.  The unit initialization logic will reset these for}
  41. { an enhanced keyboard if one is detected.                      }
  42. {---------------------------------------------------------------}
  43.   QueryKeybd : Byte = $01;
  44.   ReadKeybd  : Byte = $00;
  45. {===============================================================}
  46. FUNCTION GetExtKey : Word; ASSEMBLER;
  47. {---------------------------------------------------------------}
  48. { Reads extended keyboard and returns SCAN/CHAR code.           }
  49. { Returns 0 IF no key is available.                             }
  50. { Uses ASSEMBLER function to eliminate overhead associated with }
  51. { normal Pascal calls.                                          }
  52. {---------------------------------------------------------------}
  53. ASM
  54.   mov ah, QueryKeybd          { 00h for 84-key; 10h for 101-key }
  55.   int 16h
  56.   mov ax, 0
  57.   jz  @@out
  58.   mov ah, ReadKeybd           { 01h for 84-key; 11h for 101-key }
  59.   int 16h
  60. @@out:
  61. END;
  62. {===============================================================}
  63. PROCEDURE GetKeyEvent(VAR Event : TEvent );
  64. {---------------------------------------------------------------}
  65. { Similar to DRIVERS.GetKeyEvent, except:                       }
  66. {   1) Knows about enhanced keyboards                           }
  67. {   2) Can eliminate 'duplicate' key codes                      }
  68. {---------------------------------------------------------------}
  69. BEGIN
  70.   Event.KeyCode := GetExtKey;
  71.   IF Event.KeyCode <> 0 THEN BEGIN
  72.     Event.What := evKeyDown;
  73.     IF (FilterDuplicateKeys AND (Event.KeyCode AND $00FF = $00E0))
  74.     THEN
  75.       Event.KeyCode := Event.KeyCode AND $FF00;
  76.   END
  77.   ELSE
  78.     Event.What := evNothing;    { Don't assume it was evNothing!}
  79. END;
  80. {================================================================}
  81. FUNCTION IsExtKeyboard : Boolean; ASSEMBLER;
  82. {----------------------------------------------------------------}
  83. { Uses a BIOS vendor's recommended method for ascertaining       }
  84. { whether an enhanced keyboard is installed.                     }
  85. { (May not work on EVERY machine/keyboard combination.)          }
  86. {          Refer also to the comments in the text.               }
  87. {----------------------------------------------------------------}
  88. CONST
  89.   BogusKey = $FEDC;
  90.  
  91. ASM
  92. @@flush_it:                 { (1) Flush all keys from kbd buffer }
  93.    mov  ah, 1                          { Is there a key waiting? }
  94.    int  16h
  95.    jz   @@flush_done                              { NO--continue }
  96.    mov  ah, 0                            { YES--Read waiting key }
  97.    int  16h
  98.    jmp  @@flush_it                            { Loop until done! }
  99. @@flush_done:
  100.    mov  ah, 5               { (2) Load a dummy key in the buffer }
  101.    mov  cx, BogusKey
  102.    int  16h
  103.    jc   @@noext             { (3a) Stuff failed, NOT an ext. kbd }
  104.    or   al, al
  105.    jnz  @@noext             { (3b) Stuff failed, NOT an ext. kbd }
  106.    mov  ah, 10h             { (4) Now try to read back character }
  107.    int  16h
  108.    cmp  ax, BogusKey
  109.    jne  @@noext              { (5a) Read failed, NOT an ext. kbd }
  110.    mov  ax, 1               { (5b) Succeeded! It IS an ext. kbd! }
  111.    jmp  @@done
  112. @@noext:
  113.    xor  ax, ax
  114. @@done:
  115. END;
  116. {===============================================================}
  117. {       U N I T   I N I T I A L I Z A T I O N   L O G I C       }
  118. {===============================================================}
  119. { If this unit is used, the logic tests for an enhanced         }
  120. { keyboard, and if found, sets up the appropriate BIOS INT 16h  }
  121. { functions.                                                    }
  122. {---------------------------------------------------------------}
  123. BEGIN
  124.   IF IsExtKeyBoard THEN BEGIN
  125.     ExtKeyBoardInstalled := TRUE;
  126.     QueryKeybd := $11;             { Extended Query Keyb'd code }
  127.     ReadKeybd  := $10;              { Extended Read Keyb'd code }
  128.   END;
  129. END.
  130.